package com.example.music_player

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.music_player.data.PlayHistory
import java.text.SimpleDateFormat
import java.util.Locale

class PlayHistoryAdapter(private var historyList: List<PlayHistory>) : 
    RecyclerView.Adapter<PlayHistoryAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val songName: TextView = view.findViewById(R.id.tv_song_name)
        val playTime: TextView = view.findViewById(R.id.tv_play_time)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_play_history, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val history = historyList[position]
        holder.songName.text = history.songName
        holder.playTime.text = SimpleDateFormat(
            "yyyy-MM-dd HH:mm", Locale.getDefault()
        ).format(history.playTime)
    }

    override fun getItemCount() = historyList.size

    fun updateData(newList: List<PlayHistory>) {
        historyList = newList
        notifyDataSetChanged()
    }
} 